/*
 * SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
 *
 * SPDX-License-Identifier: MulanPSL-2.0
 */
use crate::common::*;
extern "C" {
    fn malloc(_: libc::c_ulong) -> *mut libc::c_void;
    fn free(_: *mut libc::c_void);
    fn strdup(_: *const libc::c_char) -> *mut libc::c_char;
    fn sudo_debug_exit_v1(
        func: *const libc::c_char,
        file: *const libc::c_char,
        line: libc::c_int,
        subsys: libc::c_int,
    );
    fn sudo_debug_exit_ptr_v1(
        func: *const libc::c_char,
        file: *const libc::c_char,
        line: libc::c_int,
        subsys: libc::c_int,
        ret: *const libc::c_void,
    );
    static mut sudoers_subsystem_ids: [libc::c_uint; 0];
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct sudoers_string {
    pub entries: C2RustUnnamed,
    pub str_0: *mut libc::c_char,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed {
    pub stqe_next: *mut sudoers_string,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct sudoers_str_list {
    pub stqh_first: *mut sudoers_string,
    pub stqh_last: *mut *mut sudoers_string,
    pub refcnt: libc::c_uint,
}
#[no_mangle]
pub fn sudoers_string_alloc(mut s: *const libc::c_char) -> *mut sudoers_string {
    let mut cs: *mut sudoers_string = std::ptr::null_mut::<sudoers_string>();
    unsafe {
        debug_decl!(SUDOERS_DEBUG_UTIL!());

        cs = malloc(::core::mem::size_of::<sudoers_string>() as libc::c_ulong)
            as *mut sudoers_string;
        if !cs.is_null() {
            (*cs).str_0 = strdup(s);
            if ((*cs).str_0).is_null() {
                free(cs as *mut libc::c_void);
                cs = std::ptr::null_mut::<sudoers_string>();
            }
        }

        debug_return_ptr!(cs);
    } //unsafe
}
#[no_mangle]
pub fn sudoers_string_free(mut cs: *mut sudoers_string) {
    unsafe {
        if !cs.is_null() {
            free((*cs).str_0 as *mut libc::c_void);
            free(cs as *mut libc::c_void);
        }
    } //unsafe
}
#[no_mangle]
pub fn str_list_alloc() -> *mut sudoers_str_list {
    let mut strlist: *mut sudoers_str_list = std::ptr::null_mut::<sudoers_str_list>();
    unsafe {
        debug_decl!(SUDOERS_DEBUG_UTIL!());

        strlist = malloc(::core::mem::size_of::<sudoers_str_list>() as libc::c_ulong)
            as *mut sudoers_str_list;
        if !strlist.is_null() {
            (*strlist).stqh_first = std::ptr::null_mut::<sudoers_string>();
            (*strlist).stqh_last = &mut (*strlist).stqh_first;
            (*strlist).refcnt = 1 as libc::c_int as libc::c_uint;
        }

        debug_return_ptr!(strlist);
    } //unsafe
}
#[no_mangle]
pub fn str_list_free(mut v: *mut libc::c_void) {
    let mut strlist: *mut sudoers_str_list = v as *mut sudoers_str_list;
    let mut first: *mut sudoers_string = std::ptr::null_mut::<sudoers_string>();
    unsafe {
        debug_decl!(SUDOERS_DEBUG_UTIL!());

        if !strlist.is_null() {
            (*strlist).refcnt = ((*strlist).refcnt).wrapping_sub(1);
            if (*strlist).refcnt == 0 as libc::c_int as libc::c_uint {
                loop {
                    first = (*strlist).stqh_first;
                    if first.is_null() {
                        break;
                    }
                    (*strlist).stqh_first = (*(*strlist).stqh_first).entries.stqe_next;
                    if ((*strlist).stqh_first).is_null() {
                        (*strlist).stqh_last = &mut (*strlist).stqh_first;
                    }
                    sudoers_string_free(first);
                }
                free(strlist as *mut libc::c_void);
            }
        }

        debug_return!();
    } //unsafe
}
